package com.ruoyi.project.system.nhmtcnn.service.impl;
import com.ruoyi.project.system.base_family_info.mapper.BaseFamilyInfoMapper;
import com.ruoyi.project.system.farmer.domain.Farmer;
import com.ruoyi.project.system.farmer.mapper.FarmerMapper;
import com.ruoyi.project.system.nhmtcnn.domain.FaceDetectionInfo;
import com.ruoyi.project.system.nhmtcnn.service.INhmtcnnService;
import com.ruoyi.project.tool.mtcnn.PythonProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

@Service
public class NHmtcnnServiceImpl implements INhmtcnnService {

    @Autowired
    private PythonProcessor pythonProcessor;

    @Autowired
    private FarmerMapper farmerMapper;
    @Resource
    private BaseFamilyInfoMapper baseFamilyInfoMapper;
    @Override
//    没用的废物方法
    public void Base64ToImage(String imageBase64) {
        try {
//            String encodedBase64String = "";
            String decodedBase64String = URLDecoder.decode(imageBase64, StandardCharsets.UTF_8.name());

            String base64Image = decodedBase64String.split(",")[1]; // 如果有前缀，用逗号分隔并取第二部分

            String base64Image2 = base64Image.replaceAll("\\s", "");

            byte[] imageBytes = Base64.getDecoder().decode(base64Image2);

            try (ByteArrayInputStream bis = new ByteArrayInputStream(imageBytes)) {
                BufferedImage image = ImageIO.read(bis);
                File outputFile = new File("output.jpg");
                ImageIO.write(image, "jpg", outputFile);
                System.out.println("Image saved as output.jpg");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Farmer selectFarmByName(String Name) {
        return farmerMapper.selectFarmByName(Name);
    }

    @Override
    public FaceDetectionInfo detectImgByFaceNet(String img_path) {
        StringBuilder output = new StringBuilder();
        FaceDetectionInfo faceDetectionInfo = new FaceDetectionInfo();
        try {
            // 要执行的 CMD 命令
            String command = "cmd";
            String arg = "/c";
            String cmdCommand = "cd detect_face && python detect_img_by_mtcnn.py --img_path " + img_path;
            // python detect_img_by_mtcnn.py --img_path D:\\NH\\Img_face\\1.jpg
            // 使用 ProcessBuilder 构建命令
            ProcessBuilder processBuilder = new ProcessBuilder(command, arg, cmdCommand);

            // 启动进程
            Process process = processBuilder.start();

            // 获取命令的输出流
            InputStream inputStream = process.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            // 读取输出结果
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                output.append(line).append("\n");
            }

            // 等待命令执行完成
            int exitCode = process.waitFor();

            faceDetectionInfo.setName(output.toString());
            faceDetectionInfo.setExitCode(exitCode);

            // 关闭资源
            bufferedReader.close();
            inputStreamReader.close();
            inputStream.close();

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        return faceDetectionInfo;
    }

    /**
     * 查询农户个人信息
     *
     * @param id 农户管理ID
     *
     */

//    public Farmer selectFarmByName(String Name)
//    {
//        //直接将需要的属性写到farmer里面，用时从另一个对象中拿
//        Farmer farmer = farmerMapper.selectFarmByName(Name);
//        BaseFamilyInfo baseFamilyInfo = baseFamilyInfoMapper.selectBaseFamilyInfoByFid(farmer.getFid());
//        //还差adm_village 和num
//        farmer.setFid(baseFamilyInfo.getFid());
//        farmer.setName(baseFamilyInfo.getName());
//        farmer.setNatVillage(baseFamilyInfo.getNatVillage());
//        farmer.setState(baseFamilyInfo.getState());
//        farmer.setArea(baseFamilyInfo.getArea());
//        farmer.setPhone(baseFamilyInfo.getPhone());
//        farmer.setFamilyType(baseFamilyInfo.getFamilyType());
//        farmer.setTwoArea(baseFamilyInfo.getTwoArea());
//        farmer.setOtherArea(baseFamilyInfo.getOtherArea());
//        return farmer;
//    }

}
